package Solution.problem002;

import common.ListNode;

/**
 * @program Leetcode
 * @description: 两数求和
 * @author: lishangsheng
 * @create: 2018/11/05 18:21
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1==null){
            return l2;
        }else if(l2==null){
            return l1;
        }
        return sumNode(null,l1,l2,0);

    }
    private ListNode sumNode(ListNode node,ListNode l1, ListNode l2,int carry) {
        int sum;
        if (l1 == null && l2 == null) {
            if (carry != 0) {
                sum = carry;
            } else {
                return node;
            }
        } else if (l2 == null) {
            sum = l1.val + carry;
        } else if (l1 == null) {
            sum = l2.val + carry;
        } else {
            sum = l1.val + l2.val + carry;
        }
        node = new ListNode(sum % 10);
        sumNode(node.next, l1.next, l2.next, sum / 10);
        return node;
    }
    public static void main(String[] args){

    }
}
